package cwb1;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * This class put and get courses from the database. 
 * @author Aurora
 *
 */
public class DivisionRegistry {

		//The parent key for all courses in the database.
		private Key allDivisions = KeyFactory.createKey(Division.staticGetKind(), "allDivisions");

		//design pattern singleton.
		private static DivisionRegistry instance = new DivisionRegistry();
		private DivisionRegistry()
		{
		}
		
		/**
		 * Returns the CourseEngine object.
		 * @return The courseEngine.
		 */
		public static DivisionRegistry getInstance()
		    {
		        return instance;
		    }
		
		public void add(Division division) {
			Entity entity = new Entity(division.getKind(), division.getId(), allDivisions);
			DatabaseCommunicator.getInstance().put(entity, division.getProperties());
		}
		
		/**
		 * Returns the course with the given Id.
		 * @return The course with the courseId.
		 * @throws EntityNotFoundException When the course is not found with the given Id.
		 */
		
		public Division get(String DivisionId) throws EntityNotFoundException {
			Key divisionKey = KeyFactory.createKey(allDivisions, Division.staticGetKind(), DivisionId);
			return DivisionFactory.createDivision(DatabaseCommunicator.getInstance().get(divisionKey).getProperties());
		}
		
		/**
		 * Deletes the course with the given courseId.
		 */
		
		public void delete(String divisionId) {
			Key divisionKey = KeyFactory.createKey(allDivisions, Division.staticGetKind(), divisionId);
			DatabaseCommunicator.getInstance().delete(divisionKey);
		}
		
		public void update(Division division) throws EntityNotFoundException {
			Key divisionKey = KeyFactory.createKey(allDivisions, division.getKind(), division.getId());
			DatabaseCommunicator.getInstance().put(DatabaseCommunicator.getInstance().get(divisionKey), division.getProperties());
		}
		
		/**
		 * Returns all the courses in the database.
		 * @return A list with all the courses in the database.
		 */
		public List<Division> getAll() {
			ArrayList<Division> courses = new ArrayList<Division>();
			for(Entity lusEntity : DatabaseCommunicator.getInstance().getAll(allDivisions, Course.staticGetKind())) {
				courses.add(DivisionFactory.createDivision(lusEntity.getProperties()));
			}
			return courses;
		}
		
		
		public List<Division> getAllType(long type) {
			ArrayList<Division> divisionTypes = new ArrayList<Division>();
			for(Entity lusEntity : DatabaseCommunicator.getInstance().getAll(allDivisions, Course.staticGetKind())) {
				Division temp = DivisionFactory.createDivision(lusEntity.getProperties());
				if(temp.getDivisionType() == type)
				divisionTypes.add(temp);
			}	
			return divisionTypes;
		}
		
		public List<Course> getAllUser(String userId) {
			ArrayList<Course> userCourses = new ArrayList<Course>();
			for(Entity lusEntity : DatabaseCommunicator.getInstance().getAll(allDivisions, Course.staticGetKind())) {
				Course temp = (Course) DivisionFactory.createDivision(lusEntity.getProperties());
				if(temp.checkStudent(userId))
				{
					userCourses.add(temp);
				}
			}
			return userCourses;
		}
}
